package com.hunttown.mes.impl;

import com.hunttown.mes.convert.AnalysisManageClickLogConverts;
import com.hunttown.mes.rpc.api.AnalysisManageClickLogRpcService;
import com.hunttown.mes.rpc.domain.AnalysisManageClickLogDTO;
import com.hunttown.mes.rpc.domain.query.AnalysisManageClickLogDTOQuery;
import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.service.AnalysisManageClickLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.AnalysisManageClickLogConverts.convert;

/**
 * created by wangjunfu on 2018-11-26 15:05:13
 */
@Service
public class AnalysisManageClickLogRpcServiceImpl implements AnalysisManageClickLogRpcService {

    private final Logger logger = LoggerFactory.getLogger(AnalysisManageClickLogRpcServiceImpl.class);

    private final AnalysisManageClickLogService analysisManageClickLogService;

    @Autowired
    public AnalysisManageClickLogRpcServiceImpl(AnalysisManageClickLogService analysisManageClickLogService) {
        this.analysisManageClickLogService = analysisManageClickLogService;
    }

    @Override
    public AnalysisManageClickLogDTO insert(AnalysisManageClickLogDTO analysisManageClickLogDTO) {
        try {
            return convert(analysisManageClickLogService.insert(AnalysisManageClickLogConverts.convert(analysisManageClickLogDTO)));
        } catch (Exception e) {
            logger.error("AnalysisManageClickLog create failed.", e);
            throw new RuntimeException("AnalysisManageClickLog create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(AnalysisManageClickLogDTO analysisManageClickLogDTO) {
        try {
            return analysisManageClickLogService.updateInfoById(AnalysisManageClickLogConverts.convert(analysisManageClickLogDTO));
        } catch (Exception e) {
            logger.error("AnalysisManageClickLog update by id failed.", e);
            throw new RuntimeException("AnalysisManageClickLog update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(AnalysisManageClickLogDTOQuery analysisManageClickLogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (analysisManageClickLogDTOQuery.getW_id() != null && analysisManageClickLogDTOQuery.getW_id() > 0) {
                query.put("w_id", analysisManageClickLogDTOQuery.getW_id());
            }

            if (analysisManageClickLogDTOQuery.getIds() != null && !analysisManageClickLogDTOQuery.getIds().equals("")) {
                query.put("ids", analysisManageClickLogDTOQuery.getIds());
            }

            return analysisManageClickLogService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("AnalysisManageClickLog update by query failed.", e);
            throw new RuntimeException("AnalysisManageClickLog update by query failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisManageClickLogDTO selectById(Integer id) {
        try {
            return convert(analysisManageClickLogService.selectById(id));
        } catch (Exception e) {
            logger.error("AnalysisManageClickLog select by id failed.", e);
            throw new RuntimeException("AnalysisManageClickLog select by id failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisManageClickLogDTO selectObjByQuery(AnalysisManageClickLogDTOQuery analysisManageClickLogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            return convert(analysisManageClickLogService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("AnalysisManageClickLog select obj by query failed.", e);
            throw new RuntimeException("AnalysisManageClickLog select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<AnalysisManageClickLogDTO> getListForPage(AnalysisManageClickLogDTOQuery analysisManageClickLogDTOQuery) {
        Query query = Query.page(analysisManageClickLogDTOQuery.getPn(), analysisManageClickLogDTOQuery.getPageSize());

        if (analysisManageClickLogDTOQuery.getIds() != null && !analysisManageClickLogDTOQuery.getIds().equals("")) {
            query.put("ids", analysisManageClickLogDTOQuery.getIds());
        }

        if (analysisManageClickLogDTOQuery.getLikeName() != null && !analysisManageClickLogDTOQuery.getLikeName().equals("")) {
            query.put("likeName", analysisManageClickLogDTOQuery.getLikeName());
        }

        if (analysisManageClickLogDTOQuery.getOrderBy() != null && analysisManageClickLogDTOQuery.getOrderBy().length() > 0) {
            query.put("orderBy", analysisManageClickLogDTOQuery.getOrderBy());
        }

        List<AnalysisManageClickLogDTO> list = convert(analysisManageClickLogService.selectByQuery(query));
        Page<AnalysisManageClickLogDTO> page = Page.from(analysisManageClickLogDTOQuery.getPn(), analysisManageClickLogDTOQuery.getPageSize(), list);
        page.setTotalItems(analysisManageClickLogService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return analysisManageClickLogService.deleteById(id);
        } catch (Exception e) {
            logger.error("AnalysisManageClickLog delete failed.", e);
            throw new RuntimeException("AnalysisManageClickLog delete failed, Please order your code.", e);
        }
    }
}